package be.kuleuven.mume.query;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

//Needs abstract for the getGenericType to work.
public abstract class FetchQueryHelper<T> extends QueryHelper<T> {
	private String stringKey;
	
	public FetchQueryHelper(String stringKey) {
		this.stringKey = stringKey;
	}
	@Override
	public T execute(PersistenceManager pm) {
		Key key = KeyFactory.stringToKey(stringKey);
		return pm.getObjectById(getGenericType(), key);
	}

	/**
	 * TODO: Fix method below
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Class<T> getGenericType() {
		Class<?> c = this.getClass();
		ParameterizedType d = (ParameterizedType)c.getGenericSuperclass();
		Type f = d.getActualTypeArguments()[0];
		return (Class<T>)f;
	}
}
